﻿Option Explicit On
Imports System.IO

Module config
    Public config_name(100) As String
    Public config_val(100) As String
    Public nconfigs As Integer

    Public Sub readConfig(ByVal name As String)
        Dim f As New FileStream(name, FileMode.OpenOrCreate)
        Dim s As New StreamReader(f)
        Dim token() As String
        Dim riga As String

        While Not s.EndOfStream
            riga = s.ReadLine()
            token = riga.Split(":")
            If token.Length = 2 Then
                If token(0).Length > 0 And token(1).Length > 0 Then
                    setConfig(token(0), token(1).Trim())
                End If
            End If
        End While

        s.Close()
        f.Close()
    End Sub

    Public Sub writeConfig(ByVal name As String)
        Dim f As New FileStream(name, FileMode.Truncate)
        Dim s As New StreamWriter(f)

        For i = 0 To nconfigs - 1
            s.WriteLine(config_name(i) & ": " & config_val(i))
        Next

        s.Close()
        f.Close()
    End Sub

    Public Sub addConfig(ByVal name As String, ByVal val As String)
        config_name(nconfigs) = name
        config_val(nconfigs) = val
        nconfigs += 1
    End Sub

    Public Function getConfig(ByVal name As String) As String
        Dim index As Integer
        index = Array.IndexOf(config_name, name)
        If index = -1 Then
            getConfig = ""
        Else
            getConfig = config_val(index)
        End If
    End Function

    Public Sub setConfig(ByVal name As String, ByVal val As String)
        Dim index As Integer
        index = Array.IndexOf(config_name, name)
        If index = -1 Then
            addConfig(name, val)
        Else
            config_val(index) = val
        End If
    End Sub

    Public Sub defaultConfig()
        setConfig("dbfile", Application.StartupPath & "/db.mdb")
        setConfig("pausa lunga", 4)
        setConfig("cattura mouse", "false")
        setConfig("ricerca nome count", 10)
        setConfig("sound pausa", "sound\cong.wav")
        setConfig("sound button", "sound\blip1.wav")
        setConfig("sound beep", "sound\beep.wav")
        setConfig("sound abort", "sound\error.wav")
        setConfig("sound exit", "sound\exit.wav")
        setConfig("sound ultima serie", "sound\ultima serie.wav")
    End Sub
End Module
